#include "01_Threadpool.hpp"
#include "06_Thread.hpp"
#include <unistd.h>
#include <stdio.h>
#include <vector>
#include <memory>
#include <functional>

using std::vector;
using std::make_unique;

void mytask(int id, int& flag)
{
    printf("id = %d, flag执行前 = %d \n", id, flag);
    sleep(1);
    flag = 1;
    printf("id = %d, flag执行后 = %d \n", id, flag);
}

void test(){
    printf("main | 开始创建Threadpool对象\n");
    ThreadPool threadpool(4, 10);

    printf("main | 开始运行Threadpool\n");
    threadpool.start();
    
    // 没有消费者的情况下，先执行创建任务会被阻塞
    printf("main | 开始创建进程\n");
    vector<int> done1(4, 0);    
    //bool flag = false;
    for(int i = 0; i < 4; ++i){
        //threadpool.addTask(mytask(i, done1[i]));              // 传参不符合规范
        //threadpool.addTask(std::bind(mytask, i, done1[i]));   // 值拷贝
        threadpool.addTask(std::bind(mytask, i, std::ref(done1[i])));   
    }

    sleep(10);

    vector<int> done2(20, 0);    
    for(int i = 0; i < 20; ++i){
        threadpool.addTask(std::bind(mytask, i, std::ref(done2[i])));   
    }
    
    printf("main | 停止运行Threadpool\n");
    threadpool.stop();
    // 检查队列
    for(auto b : done1) printf("done1 = %d\n", b);
    for(auto b : done2) printf("done2 = %d\n", b);
}

int main()
{
    test();
    return 0;
}

